<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image src="../../../static/icons/leju-logo.png"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput v-model="phoneData" type="text" maxlength="11" placeholder="用户名/电话"></wInput>
				<wInput v-model="passData" type="password" maxlength="11" placeholder="密码"></wInput>
			</view>
			<wButton bgColor="#354E44" text="登 录" :rotate="isRotate" @click.native="startLogin()"></wButton>

			<!-- 其他登录 -->
			<!-- <view class="other_login cuIcon">
				<view class="login_icon">
					<view class="cuIcon-weixin" @tap="login_weixin"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-weibo" @tap="login_weibo"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-github" @tap="login_github"></view>
				</view>
			</view> -->

			<!-- 底部信息 -->
			<view class="footer">
				<navigator url="forget" open-type="navigate">找回密码</navigator>
				<text>|</text>
				<navigator url="register" open-type="navigate">注册账号</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	var _this;
	import wInput from '@/components/watch-login/watch-input.vue' //input
	import wButton from '@/components/watch-login/watch-button.vue' //button
	import userApi from '@/api/user/index.js'
	import {
		clearUserCache
	} from '@/utils/index.js'
	
	export default {
		data() {
			return {
				//logo图片 base64
				logoImage: '',
				phoneData: '17596496508', //用户/电话
				passData: '123456', //密码
				isRotate: false, //是否加载旋转
				user: {}
			};
		},
		components: {
			wInput,
			wButton,
		},
		mounted() {
			_this = this;
			//this.isLogin();
		},
		methods: {
			isLogin() {
				
			},
			login(){
				return new Promise((resolve,rej)=>{
					userApi.doLogin({
						username:this.phoneData,
						password:this.passData
					}).then(res=>{
						if(res.success == true){
							// 清除旧信息
							uni.clearStorageSync();
							uni.setStorageSync("leju_token",res.data.token);
							resolve(true)
						}else{
							rej(false)
						}
					})
				})
			},
			getUserInfo(){
				return new Promise((resolve,rej)=>{
					userApi.getMemberInfo()
					.then(res =>{
						if(res.success == true){
							uni.setStorageSync("leju_user",res.data.userInfo);
							uni.showToast({
								title:"登录成功,1s后跳转到个人中心"
							})
							setTimeout(()=>{
								uni.reLaunch({
									url: '../index'
								})
							},1000)
							
							resolve()
						}else{
							reject()
						}
					})
					
				})
			},
			async startLogin() {
				//登录
				if (this.isRotate) {
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.phoneData.length == "") {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '用户名不能为空'
					});
					return;
				}
				if (this.passData.length < 5) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '密码不正确'
					});
					return;
				}


				_this.isRotate = true
				setTimeout(function() {
					_this.isRotate = false
				}, 3000)
				// uni.showLoading({
				// 	title: '登录中'
				// });
				var result = await this.login();
				if(result){
					await this.getUserInfo();
				}else{
					uni.showToast({
						title:"登录失败,请检查用户名和密码!"
					})
				}
				//执行登陆
				// this.$store.dispatch('user/doLogin', {
				// 		username: this.phoneData,
				// 		password: this.passData
				// 	})
				// 	.then(res => {
				// 		uni.showToast({
				// 			icon: 'success',
				// 			position: 'bottom',
				// 			title: '登录成功'
				// 		});
				// 		setTimeout(() => {
				// 			uni.reLaunch({
				// 				url: '../index'
				// 			})
				// 		}, 200)

				// 	}).catch(err => {
				// 		uni.showToast({
				// 			icon: 'error',
				// 			position: 'bottom',
				// 			title: err
				// 		});
				// 	})
				// uni.hideLoading();

			},
			login_weixin() {
				//微信登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_weibo() {
				//微博登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_github() {
				//github登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			}
		}
	}
</script>

<style>
	@import url("../../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>
